package com.blog.wu.myblog.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.*;
import java.util.Enumeration;

/**
 * @Author wuyanshen
 * @Date 2018-10-12 下午5:23
 * @Discription 描述
 */
public class HttpUtils {

    public final static String WIN_OS = "WINDOWS";
    public final static String MAC_OS = "MAC";
    public final static String LINUX_OS = "LINUX";
    public final static String OTHER_OS = "OTHER";

    /**
     * 获取用户的ip地址
     *
     * @param request
     * @return java.lang.String
     * @Date 2018/10/12 下午5:23
     * @Author wuyanshen
     */
    public static String getIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = ip.indexOf(",");
            if (index != -1) {
                return ip.substring(0, index);
            } else {
                return ip;
            }
        }
        ip = request.getHeader("X-Real-IP");
        if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            return ip;
        }
        return request.getRemoteAddr();
    }

    /**
     * 获取本机mac地址
     *
     * @return void
     * @Date 2018/10/15 下午1:10
     * @Author wuyanshen
     */
    public static String getLocalMac() {
        try {
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            byte[] mac = null;
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                if (netInterface.isLoopback() || netInterface.isVirtual() || netInterface.isPointToPoint() || !netInterface.isUp()) {
                    continue;
                } else {
                    mac = netInterface.getHardwareAddress();
                    if (mac != null) {
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < mac.length; i++) {
                            sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
                        }
                        if (sb.length() > 0) {
                            return sb.toString();
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("MAC地址获取失败" + e);
        }
        return "";
    }

    public static String hexByte(byte b) {
        String s = "000000" + Integer.toHexString(b);
        return s.substring(s.length() - 2);
    }

    /**
     * 获取当前的操作系统
     *
     * @param
     * @return java.lang.String
     * @Date 2018/10/15 下午1:19
     * @Author wuyanshen
     */
    public static String getOS() {
        if (SystemUtils.IS_OS_WINDOWS) {
            return WIN_OS;
        }
        if (SystemUtils.IS_OS_MAC || SystemUtils.IS_OS_MAC_OSX) {
            return MAC_OS;
        }
        if (SystemUtils.IS_OS_UNIX) {
            return LINUX_OS;
        }
        return OTHER_OS;
    }
}
